*     ***************************************************************** *;
*     ***************************************************************** *;
*       File-Name:      individual_level.do                             *;
*       Date:           12/28/2020                                      *;
*       Authora:        Baccini, Guidi, Poletti, and Yildirim           *;
*       Purpose:        Replication of "Trade Liberalization and Labor  *;
*						Market Institutions"                            *;                                                                      
*       Input Files:    individual_level.dta 					        *; 
*       Output Files:   individual_level_logfile.log                    *;
*       Machine:        Office                                          *;
*       Program: 		Stata 15                                        *;
*     ****************************************************************  *;
*     ****************************************************************  *;

* Setting initial commands
clear
clear matrix
clear mata
set maxvar 50000
set matsize 11000
set more off 

* Loading the dataset
*cd "Replication File"
use individual_level, clear
log using individual_level_logfile.log

* Building matrices of controls
global controls1 gndr c.gndr#c.coord_dummy eduyrs c.eduyrs#c.coord_dummy lrscale c.lrscale#c.coord_dummy s1_skill_spec c.s1_skill_spec#c.coord_dummy
global controls2 c.gndr#c.instrument1n c.eduyrs#c.instrument1n c.lrscale#c.instrument1n c.s1_skill_spec#c.instrument1n
global controls3 c.instrument1n#c.patents c.instrument1n#c.corruption_1996 c.instrument1n#c.pr c.instrument1n#c.migration c.instrument1n#c.unempl_ilo_1995 c.instrument1n#c.euro c.instrument1n#c.credit_private_sector c.instrument1n#c.se c.instrument1n#c.tax

* Table 3 and Figures 6, H3, and H4
*1
reg gincdif_dummy instrument1n  c.instrument1n#c.coord_dummy i.id_country_wave $controls1 $controls2 $controls3 i.id_country_wave i.id_nace [pweight= dweight ], r cluster(id_country)
outreg2 using table3.xls, bdec(3) tdec(3) addstat(`e(r2_p)') addtext(Country-wave fixed effects, Yes, Industry fixed effects, Yes, Individual Controls, Yes, Confounders, Yes) keep(instrument1n c.instrument1n#c.coord_dummy s1_skill_spec c.s1_skill_spec#c.instrument1n c.s1_skill_spec#c.coord_dummy c.s1_skill_spec#c.instrument1n#c.coord_dummy) label
margins, at(instrument1n=(0(10)60)) over(coord_dummy) atmeans level(90)  estimtolerance(1e-2)
marginsplot, addplot((histogram instrument1n if instrument1n<=60 & coord_dummy == 0, percent fcolor(none) lcolor(gs1) yaxis(2) yscale(alt  axis(2))) (histogram instrument1n if instrument1n<=60 & coord_dummy == 1, percent fcolor(none) lcolor(red) yaxis(2) yscale(alt  axis(2)))) level(90)
graph save Graph "C:\Users\lbacci\Dropbox\Projects\NNTT_VoC\Draft\LaTex\IO\Revisions\000 Check\Figure6.gph", replace
graph save Graph "C:\Users\lbacci\Dropbox\Projects\NNTT_VoC\Draft\LaTex\IO\Revisions\000 Check\Figure6.pdf", replace
*2
reg gincdif_dummy instrument1n  c.instrument1n#c.coord_dummy i.id_country_wave $controls1 $controls2 $controls3 i.id_country_wave i.id_nace if eduyrs<=16 [pweight= dweight ], r cluster(id_country)
outreg2 using table3.xls, bdec(3) tdec(3) addstat(`e(r2_p)') addtext(Country-wave fixed effects, Yes, Industry fixed effects, Yes, Individual Controls, Yes, Confounders, Yes) keep(instrument1n c.instrument1n#c.coord_dummy s1_skill_spec c.s1_skill_spec#c.instrument1n c.s1_skill_spec#c.coord_dummy c.s1_skill_spec#c.instrument1n#c.coord_dummy) label append
margins, at(instrument1n=(0(10)60)) over(coord_dummy) atmeans level(90)  estimtolerance(1e-2)
marginsplot, addplot((histogram instrument1n if instrument1n<=60 & coord_dummy == 0, percent fcolor(none) lcolor(gs1) yaxis(2) yscale(alt  axis(2))) (histogram instrument1n if instrument1n<=60 & coord_dummy == 1, percent fcolor(none) lcolor(red) yaxis(2) yscale(alt  axis(2)))) level(90)
graph save Graph "C:\Users\lbacci\Dropbox\Projects\NNTT_VoC\Draft\LaTex\IO\Revisions\000 Check\FigureH3.gph", replace
graph save Graph "C:\Users\lbacci\Dropbox\Projects\NNTT_VoC\Draft\LaTex\IO\Revisions\000 Check\FigureH3.pdf", replace
*3
reg gincdif_dummy instrument1n  c.instrument1n#c.coord_dummy i.id_country_wave $controls1 $controls2 $controls3 i.id_country_wave i.id_nace if eduyrs>16 [pweight= dweight ], r cluster(id_country)
outreg2 using table3.xls, bdec(3) tdec(3) addstat(`e(r2_p)') addtext(Country-wave fixed effects, Yes, Industry fixed effects, Yes, Individual Controls, Yes, Confounders, Yes) keep(instrument1n c.instrument1n#c.coord_dummy s1_skill_spec c.s1_skill_spec#c.instrument1n c.s1_skill_spec#c.coord_dummy c.s1_skill_spec#c.instrument1n#c.coord_dummy) label append
margins, at(instrument1n=(0(10)60)) over(coord_dummy) atmeans level(90)  estimtolerance(1e-2)
marginsplot, addplot((histogram instrument1n if instrument1n<=60 & coord_dummy == 0, percent fcolor(none) lcolor(gs1) yaxis(2) yscale(alt  axis(2))) (histogram instrument1n if instrument1n<=60 & coord_dummy == 1, percent fcolor(none) lcolor(red) yaxis(2) yscale(alt  axis(2)))) level(90)
graph save Graph "C:\Users\lbacci\Dropbox\Projects\NNTT_VoC\Draft\LaTex\IO\Revisions\000 Check\FigureH4.gph", replace
graph save Graph "C:\Users\lbacci\Dropbox\Projects\NNTT_VoC\Draft\LaTex\IO\Revisions\000 Check\FigureH4.pdf", replace

* Table H1
reg gincdif_dummy instrument1n  c.instrument1n#c.coord_dummy i.id_country_wave $controls1 $controls2 $controls3 i.id_country_wave i.id_nace [pweight= dweight ], r cluster(id_country)
sum gincdif_dummy instrument1n $controls1 $controls2 $controls3 if e(sample)
